<!-- src/views/FrontFrame.vue -->
<template>
  <div class="front-container">
    <TopBar />
    <div class="content">
      <MenuLine />
      <el-carousel height="400px" class="carousel">
        <el-carousel-item
          v-for="(item, index) in carouselImages"
          :key="index"
          @click="item.id ? $router.push(`/news/${item.id}`) : null"
          :class="{ 'clickable': item.id }"
        >
          <img
            :src="item.url"
            alt="新闻封面"
            style="width: 100%; height: 100%; object-fit: cover;"
            @error="handleImageError"
          />
        </el-carousel-item>
      </el-carousel>
      <div class="news-section">
        <div class="news-header">
          <h2>新闻资讯</h2>
          <el-link type="primary" @click="$router.push('/news')">更多</el-link>
        </div>
        <el-row :gutter="30">
          <el-col
            v-for="news in newsList"
            :key="news.id"
            :span="8"
            class="news-card"
            @click="$router.push(`/news/${news.id}`)"
          >
            <el-card :body-style="{ padding: '0px' }">
              <img
                :src="news.coverUrl || defaultImage"
                class="news-image"
                @error="handleImageError"
              />
              <div class="news-title">
                <span>{{ news.title }}</span>
              </div>
            </el-card>
          </el-col>
        </el-row>
      </div>
      <div class="announcement-section">
        <div class="announcement-header">
          <h2>公告信息</h2>
          <el-link type="primary" @click="$router.push('/announcement')">更多</el-link>
        </div>
        <el-card shadow="hover">
          <el-collapse>
            <el-collapse-item
              v-for="announcement in announcementList"
              :key="announcement.id"
              :title="announcement.title"
            >
              <div>{{ announcement.content }}</div>
            </el-collapse-item>
          </el-collapse>
        </el-card>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue';
import { ElMessage } from 'element-plus';
import { useNewsStore } from '@/stores/news';
import { useAnnouncementStore } from '@/stores/announcement';
import { usePermissionStore } from '@/stores/permission';
import TopBar from '@/components/TopBar.vue';
import MenuLine from '@/components/MenuLine.vue';

const newsStore = useNewsStore();
const announcementStore = useAnnouncementStore();
const permissionStore = usePermissionStore();

const defaultImage = 'https://picsum.photos/400/200?random=0';
const newsList = ref([]);
const announcementList = ref([]);

// 从新闻数据的 coverUrl 和 id 动态生成轮播图
const carouselImages = computed(() => {
  const images = newsList.value.slice(0, 3).map(news => ({
    url: news.coverUrl || defaultImage,
    id: news.id,
  }));
  // 如果新闻不足3条，用 defaultImage 补齐（无 id）
  while (images.length < 3) {
    images.push({ url: defaultImage, id: null });
  }
  return images;
});

const handleImageError = (e: Event) => {
  (e.target as HTMLImageElement).src = defaultImage;
};

onMounted(async () => {
  try {
    await permissionStore.fetchPermissions();
    const newsData = await newsStore.fetchNewsList();
    newsList.value = newsData.slice(0, 9);
    const announcementData = await announcementStore.fetchAnnouncements();
    announcementList.value = announcementData;
  } catch (error: any) {
    ElMessage.error('加载数据失败: ' + error.message);
  }
});
</script>

<style scoped>
.front-container {
  min-height: 100vh;
  background-color: #f5f7fa;
  padding-bottom: 60px;
}

.content {
  padding-top: 60px;
  background-color: transparent;
}

.carousel {
  width: 80%;
  margin: 20px auto;
}

.clickable {
  cursor: pointer;
}

.news-section {
  width: 80%;
  margin: 20px auto;
}

.news-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.news-header h2 {
  font-size: 24px;
  color: #333;
  flex-grow: 1;
  text-align: center;
}

.news-header .el-link {
  flex-shrink: 0;
}

.news-card {
  margin-bottom: 20px;
  cursor: pointer;
}

.news-image {
  width: 100%;
  height: 200px;
  object-fit: cover;
}

.news-title {
  padding: 14px;
  min-height: 50px;
  display: flex;
  align-items: center;
}

.news-title span {
  font-size: 14px;
  color: #333;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
  line-height: 1.5;
}

.announcement-section {
  width: 80%;
  margin: 40px auto 0 auto;
}

.announcement-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.announcement-header h2 {
  font-size: 24px;
  color: #333;
  flex-grow: 1;
  text-align: center;
}

.announcement-header .el-link {
  flex-shrink: 0;
}

.announcement-section .el-card {
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.el-collapse {
  border: none;
  background-color: transparent;
}

.el-collapse-item__header {
  font-size: 16px;
  color: #333;
  padding: 10px 20px;
  background-color: transparent;
  border-bottom: 1px solid #ebeef5;
}

.el-collapse-item__content {
  padding: 10px 20px;
  font-size: 14px;
  color: #666;
  line-height: 1.6;
}
</style>